<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace console\controllers;

use app\modules\rbac\models\User;
use yii\console\Controller;
use yii\console\ExitCode;
use yii;

/**
 * ldap同步相关脚本
 *
 * This command is provided as an example for you to learn how to create console commands.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class LdapController extends Controller
{
    /**
     * 导入用户
     * @param string $type the message to be echoed.
     * @return int Exit code
     */
    public function actionImport($type = 'active')
    {
        if(!method_exists(__CLASS__, $type)){
            return ExitCode::USAGE;
        }
        $ad = new \Adldap\Adldap();
        $ad->addProvider(Yii::$app->params['ldap'][$type]);

        try {
            // If a successful connection is made to your server, the provider will be returned.
            $provider = $ad->connect();
            $users = $provider->search()->get();//var_dump($users);die;
            $this->$type($users);
        } catch (\Adldap\Auth\BindException $e) {

            // There was an issue binding / connecting to the server.

        }

        return ExitCode::USAGE;
    }

    private function active($users)
    {
        $now_users = $this->_getAllUsers();
        $user = new User();
        foreach ($users as $value)
        {
            $username = $value->samaccountname[0];
            $realname = $value->cn[0];
            $email = $value->userprincipalname[0];
            if(isset($now_users[$username])){
                //有这个用户,但是离职了,可能是离职后重新入职,username没变
                if($now_users[$username] == User::STATUS_INACTIVE){
                    $bool = User::updateAll(['status'=>User::STATUS_ACTIVE], 'username=:username', ['username'=>$username]);
                    if($bool){
                        echo "[INFO] $username : update success\n";
                    }else{
                        echo "[ERROR] $username : update fail\n";
                    }
                }
            }else{
                $user->setIsNewRecord(true);
                $user->id = null;
                $user->is_ldap = 1;
                $user->username = $username;
                $user->realname = $realname;
                $user->email = $email;
                $bool = $user->save();
                if($bool){
                    echo "[INFO] $username : insert success\n";
                }else{
                    echo "[ERROR] $username : insert fail\n";
                }
            }
        }
        echo "end\n";
    }

    private function level($users)
    {
        $now_users = $this->_getAllUsers();
        foreach ($users as $value) {
            $username = $value->samaccountname[0];
            if (isset($now_users[$username]) && $now_users[$username] == User::STATUS_ACTIVE) {
                $bool = User::updateAll(['status'=>User::STATUS_INACTIVE], 'username=:username', ['username'=>$username]);
                if ($bool) {
                    echo "[INFO] $username : update success\n";
                } else {
                    echo "[ERROR] $username : update fail\n";
                }
            }
        }
        echo "end\n";
    }

    private function _getAllUsers()
    {
        $res = [];
        $data = User::find()->select(['username','status'])->all();
        foreach ($data as $value)
        {
            $res[$value->username] = $value->status;
        }
        return $res;
    }
}
